package textEngine.similarityFinderTest;

public class HistogramDistance {
	
	public static double getDistance(String s1, String s2){
		/**
		 * ATTENZIONE: NON SONO STATI CONSIDERATE LE PAROLE ACCENTATE (NON DANNO CONTRIBUTO!!)
		 * NON E' STATO NORMALIZZATO!!!
		 */
		//TODO: Da considerare gli ACCENTI!!!!
		
		int[] s1Hist= computeHistogram(s1);
		int[] s2Hist= computeHistogram(s2);
		
		return euclideanDistance(s1Hist, s2Hist);
		
		
		
		
		
	}
	
	private static double euclideanDistance(int[] s1Hist, int[] s2Hist) {
		
	
		double Sum = 0.0;
		
		if(s1Hist.length!=s2Hist.length) return -1;
		for(int i=0; i<s1Hist.length;i++){
			
           Sum = Sum + Math.pow((s1Hist[i]-s2Hist[i]),2.0);
        }
        return Math.sqrt(Sum);
		
		
	}

	private static int[] computeHistogram(String s){
		
		//Non si distingua upper da lower case
		s= s.toLowerCase();
		int[] sHistogram= new int[26];
		
		//initializze
		for(int i=0; i<26; i++){
			sHistogram[i]=0;	
		}
		
		for(int i=0; i<s.length();i++){
			if(s.charAt(i)-97<26 && s.charAt(i)-97>=0){
				sHistogram[s.charAt(i)-97]++;
			}
		}
		return sHistogram;
		
	}

}
